var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebNavigation).QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindow);
var page = document.getElementById('chansenhanced-sidebar');
var ibs  = mainWindow.ChansEnhanced_ImageBoards;
var boards = {
	"b":"Random",
	"d":"Discussion",
	"rm":"Rozen Maiden",
	"guro":"Guro",
	"r":"Requests",
	"a":"Anime"
};
var IBboards = {};
for (var i in ibs)
{
	var ib = ibs[i];
	for (var j in ib.boards)
	{
		var bd = ib.boards[j];
		if (!IBboards[(bd.type?bd.type:j)])
			IBboards[(bd.type?bd.type:j)]=[];
		IBboards[(bd.type?bd.type:j)].push([i,j]);
	}
}
function ChansEnhanced_Sidebar_GroupByBoard()
{
	while(page.childNodes.length)
	{
	    page.removeChild(page.childNodes[0]);
	}

	var vbox = document.createElement('vbox');
	vbox.setAttribute('style','overflow:auto;');
	vbox.setAttribute('flex','1');
	page.appendChild(vbox);
	var gbhbox = document.createElement('hbox');
	var gbtext = document.createElement('text');
	var gbchan = document.createElement('text');
	var gbboard= document.createElement('text');
	var gbicon = document.createElement('text');
	
	gbtext.setAttribute('value' ,'Group by : ');
	gbchan.setAttribute('value' ,'[chan] ');
	gbboard.setAttribute('value','[board] ');
	gbicon.setAttribute('value' ,'[icon]');
	
	gbboard.setAttribute('style','font-weight:bold;');
	gbchan.addEventListener('click',ChansEnhanced_Sidebar_GroupByChan,true);
	gbicon.addEventListener('click',ChansEnhanced_Sidebar_GroupByIcon,true);
	
	gbhbox.appendChild(gbtext);
	gbhbox.appendChild(gbchan);
	gbhbox.appendChild(gbboard);
	gbhbox.appendChild(gbicon);
	
	vbox.appendChild(gbhbox);
	
	
	for (var i in IBboards)
	{
		if (boards[i])
		{
			var gbox = document.createElement("groupbox");
			var capt  = document.createElement("caption");
			var captT = document.createElement("text");
			var captE = document.createElement("text");		
			var captU = document.createElement("text");
			var captD = document.createElement("text");
			var captH = document.createElement("text");
			
			capt.appendChild(captT);
			capt.appendChild(captE);
			capt.appendChild(captU);
			capt.appendChild(captD);
			capt.appendChild(captH);
			gbox.appendChild(capt);
			vbox.appendChild(gbox);	
			
			capt.setAttribute('flex','1');
			captE.setAttribute('flex','1');
			captT.setAttribute('value','/'+i+'/ - '+boards[i]);
			captE.setAttribute('value',' ');
			captU.setAttribute('value','[U] ');
			captD.setAttribute('value','[D] ');
			captH.setAttribute('value','[H]');
			for (var j in IBboards[i])
			{
				var ib = ibs[IBboards[i][j][0]];
				var bd = ib.boards[IBboards[i][j][1]];
				var hbox = document.createElement("hbox");
				var img  = document.createElement("image");
				var txt  = document.createElement("text");
				
				
				hbox.appendChild(img);
				hbox.appendChild(txt);
				gbox.appendChild(hbox);
		
				img.setAttribute('src','chrome://chansenhanced/content/icons/favicons/'+(bd.icon?bd.icon:ib.icon));
				
				txt.setAttribute('value',' '+ib.title + '/' + IBboards[i][j][1] + '/' + (bd.name?(' - '+bd.name):''));
				
				var link = 'http://'+ib.host + (bd.path?bd.path:('/'+IBboards[i][j][1]+'/'));
				img.setAttribute('statustext',link);
				txt.setAttribute('statustext',link);
				img.addEventListener('click',ChansEnhanced_Sidebar_OpenBoard,true);
				txt.addEventListener('click',ChansEnhanced_Sidebar_OpenBoard,true);
			}
		}
	}
}
function ChansEnhanced_Sidebar_GroupByChan()
{
	while(page.childNodes.length)
	{
	    page.removeChild(page.childNodes[0]);
	}
	var vbox = document.createElement('vbox');
	vbox.setAttribute('style','overflow:auto;');
	vbox.setAttribute('flex','1');
	page.appendChild(vbox);
	var gbhbox = document.createElement('hbox');
	var gbtext = document.createElement('text');
	var gbchan = document.createElement('text');
	var gbboard= document.createElement('text');
	var gbicon = document.createElement('text');
	
	gbtext.setAttribute('value' ,'Group by : ');
	gbchan.setAttribute('value' ,'[chan] ');
	gbboard.setAttribute('value','[board] ');
	gbicon.setAttribute('value' ,'[icon]');
	
	gbchan.setAttribute('style','font-weight:bold;');
	gbboard.addEventListener('click',ChansEnhanced_Sidebar_GroupByBoard,true);
	gbicon.addEventListener('click',ChansEnhanced_Sidebar_GroupByIcon,true);
	
	gbhbox.appendChild(gbtext);
	gbhbox.appendChild(gbchan);
	gbhbox.appendChild(gbboard);
	gbhbox.appendChild(gbicon);
	
	vbox.appendChild(gbhbox);
	
	
	for (var i in ibs)
	{
		var ib = ibs[i];
		var gbox = document.createElement("groupbox");
		var capt  = document.createElement("caption");
		var captT = document.createElement("text");
		var captE = document.createElement("text");		
		var captU = document.createElement("text");
		var captD = document.createElement("text");
		var captH = document.createElement("text");
		
		capt.appendChild(captT);
		capt.appendChild(captE);
		capt.appendChild(captU);
		capt.appendChild(captD);
		capt.appendChild(captH);
		gbox.appendChild(capt);
		vbox.appendChild(gbox);	
		
		capt.setAttribute('flex','1');
		captE.setAttribute('flex','1');
		captT.setAttribute('value',ib.title);
		captE.setAttribute('value',' ');
		captU.setAttribute('value','[U] ');
		captD.setAttribute('value','[D] ');
		captH.setAttribute('value','[H]');
		for (var j in ib.boards)
		{
			var bd = ib.boards[j];
			var hbox = document.createElement("hbox");
			var img  = document.createElement("image");
			var txt  = document.createElement("text");
			
			hbox.appendChild(img);
			hbox.appendChild(txt);
			gbox.appendChild(hbox);
	
			img.setAttribute('src','chrome://chansenhanced/content/icons/favicons/'+(bd.icon?bd.icon:ib.icon));
			txt.setAttribute('value',' '+ib.title + '/' + j + '/' + (bd.name?(' - '+bd.name):''));

			var link = 'http://'+ib.host + (bd.path?bd.path:('/'+j+'/'));
			img.setAttribute('statustext',link);
			txt.setAttribute('statustext',link);
			img.addEventListener('click',ChansEnhanced_Sidebar_OpenBoard,true);
			txt.addEventListener('click',ChansEnhanced_Sidebar_OpenBoard,true);			
		}

	}
}
function ChansEnhanced_Sidebar_GroupByIcon()
{
	while(page.childNodes.length)
	{
	    page.removeChild(page.childNodes[0]);
	}
	var vbox = document.createElement('vbox');
	vbox.setAttribute('style','overflow:auto;');
	vbox.setAttribute('flex','1');
	page.appendChild(vbox);
	var gbhbox = document.createElement('hbox');
	var gbtext = document.createElement('text');
	var gbchan = document.createElement('text');
	var gbboard= document.createElement('text');
	var gbicon = document.createElement('text');
	
	gbtext.setAttribute('value' ,'Group by : ');
	gbchan.setAttribute('value' ,'[chan] ');
	gbboard.setAttribute('value','[board] ');
	gbicon.setAttribute('value' ,'[icon]');
	
	gbicon.setAttribute('style','font-weight:bold;');
	gbchan.addEventListener('click',ChansEnhanced_Sidebar_GroupByChan,true);
	gbboard.addEventListener('click',ChansEnhanced_Sidebar_GroupByBoard,true);
		
	gbhbox.appendChild(gbtext);
	gbhbox.appendChild(gbchan);
	gbhbox.appendChild(gbboard);
	gbhbox.appendChild(gbicon);
	
	vbox.appendChild(gbhbox);
	
	
	for (var i in ibs)
	{
		var ib = ibs[i];
		var gbox = document.createElement("groupbox");
		var capt  = document.createElement("caption");
		var captT = document.createElement("text");
		var captE = document.createElement("text");		
		var captU = document.createElement("text");
		var captD = document.createElement("text");
		var captH = document.createElement("text");
		
		capt.appendChild(captT);
		capt.appendChild(captE);
		capt.appendChild(captU);
		capt.appendChild(captD);
		capt.appendChild(captH);
		gbox.appendChild(capt);
		vbox.appendChild(gbox);	
		
		capt.setAttribute('flex','1');
		captE.setAttribute('flex','1');
		captT.setAttribute('value',ib.title);
		captE.setAttribute('value',' ');
		captU.setAttribute('value','[U] ');
		captD.setAttribute('value','[D] ');
		captH.setAttribute('value','[H]');
		var hbox = document.createElement("hbox");
		gbox.appendChild(hbox);
		var n = 0;
		for (var j in ib.boards)
		{
			if (n > 9)
			{
				n = 0;
				hbox = document.createElement("hbox");
				gbox.appendChild(hbox);
			}
			var bd = ib.boards[j];
			var img  = document.createElement("image");
			
			hbox.appendChild(img);
	
			img.setAttribute('src','chrome://chansenhanced/content/icons/favicons/'+(bd.icon?bd.icon:ib.icon));
			img.setAttribute('tooltiptext',ib.title + '/' + j + '/' + (bd.name?(' - '+bd.name):''));
			var link = 'http://'+ib.host + (bd.path?bd.path:('/'+j+'/'));
			img.setAttribute('statustext',link);
			img.addEventListener('click',ChansEnhanced_Sidebar_OpenBoard,true);
			n++;
		}

	}
}
function ChansEnhanced_Sidebar_OpenBoard(event)
{
	var href = this.getAttribute('statustext');
	if (event.button == 0)
	{
		ChansEnhanced_Sidebar_OpenInCurrentTab(href);
	}
	else if (event.button == 1)
	{
		ChansEnhanced_Sidebar_OpenInNewTab(href);
	}
}
function ChansEnhanced_Sidebar_OpenInCurrentTab(href)
{
	mainWindow.gBrowser.selectedBrowser.contentDocument.location.href = href;
}
function ChansEnhanced_Sidebar_OpenInNewTab(href)
{
	mainWindow.gBrowser.selectedTab = mainWindow.gBrowser.addTab(href,null);
}
ChansEnhanced_Sidebar_GroupByBoard();
//ChansEnhanced_Sidebar_GroupByChan();
//ChansEnhanced_Sidebar_GroupByIcon();